﻿using mozhi.smarterp.Finance.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Volo.Abp;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace mozhi.smarterp.Finance.AccountsPayables
{
    /// <summary>
    /// 应付款核销
    /// </summary>
    public class PayableSettlement : AuditedAggregateRoot<Guid>, IMultiTenant
    {
        public Guid? TenantId { get; private set; }

        public Guid PayableId { get; private set; }

        public DocumentType DocumentType { get; private set; }

        public Guid DucumentId { get; private set; }

        /// <summary>
        /// 核销币别
        /// </summary>
        public Guid SettlementCurrencyId { get; private set; }

        /// <summary>
        /// 核销原币金额
        /// </summary>
        public decimal SettlementAmountFr { get; private set; }

        /// <summary>
        /// 核销本位币金额
        /// </summary>
        public decimal SettlementAmount { get; private set; }

        /// <summary>
        /// 是否自动核销
        /// </summary>
        public bool IsAutoSettlement {  get; private set; }

        public List<PayableSettlementItem> Items { get; private set; }

        private PayableSettlement() { }

        public PayableSettlement(Guid id, Guid payableId, DocumentType documentType, Guid ducumentId, Guid settlementCurrencyId, decimal settlementAmountFr, decimal settlementAmount , bool isAutoSettlement)
        {
            Id = id;
            PayableId = payableId;
            DocumentType = documentType;
            DucumentId = ducumentId;
            SettlementCurrencyId = settlementCurrencyId;
            SettlementAmountFr = settlementAmountFr;
            SettlementAmount = settlementAmount;
            IsAutoSettlement = isAutoSettlement;

            Items = new List<PayableSettlementItem>();

            Validate();
        }

        public void SetItems(List<PayableSettlementItem> items)
        {
            if (DocumentType == DocumentType.Payment)
                throw new UserFriendlyException("应付单与付款单核销，不能存在核销明细");

            if (items == null || items.Count == 0)
                throw new UserFriendlyException("核销明细不能为空");

            Items = items;

            SettlementAmount = Items.Sum(x => x.SettlementAmount);
            SettlementAmountFr = Items.Sum(x => x.SettlementAmountFr);
        }

        #region
        private void Validate()
        {
            Check.NotDefaultOrNull<Guid>(PayableId, nameof(PayableId));
            Check.NotDefaultOrNull<Guid>(DucumentId, nameof(DucumentId));
            Check.NotDefaultOrNull<DocumentType>(DocumentType, nameof(DocumentType));
            Check.NotDefaultOrNull<decimal>(SettlementAmountFr, nameof(SettlementAmountFr));
            Check.NotDefaultOrNull<decimal>(SettlementAmount, nameof(SettlementAmount));
            Check.NotDefaultOrNull<Guid>(SettlementCurrencyId, nameof(SettlementCurrencyId));
        }
        #endregion
    }

    public class PayableSettlementItem : Entity<Guid> , IMultiTenant
    {
        public Guid? TenantId { get; private set; }

        public Guid PayableItemId { get; private set; }

        public Guid DucumentItemId {  get; private set; }

        /// <summary>
        /// 核销原币金额
        /// </summary>
        public decimal SettlementAmountFr { get; private set; }

        /// <summary>
        /// 核销本位币金额
        /// </summary>
        public decimal SettlementAmount { get; private set; }

        private PayableSettlementItem() { }

        public PayableSettlementItem(Guid id, Guid payableItemId, Guid ducumentItemId, decimal settlementAmountFr, decimal settlementAmount)
        {
            Id = id;
            PayableItemId = payableItemId;
            DucumentItemId = ducumentItemId;
            SettlementAmountFr = settlementAmountFr;
            SettlementAmount = settlementAmount;

            Validate();
        }

        #region
        private void Validate()
        {
            Check.NotDefaultOrNull<Guid>(PayableItemId, nameof(PayableItemId));
            Check.NotDefaultOrNull<Guid>(DucumentItemId, nameof(DucumentItemId));
            Check.NotDefaultOrNull<decimal>(SettlementAmountFr, nameof(SettlementAmountFr));
            Check.NotDefaultOrNull<decimal>(SettlementAmount, nameof(SettlementAmount));
        }
        #endregion
    }
}
